package com.yjt.fresh.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.HttpManager;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.listener.HttpOnNextListener;
import com.yjt.fresh.R;
import com.yjt.fresh.adpater.AdpRvAtyAddresses;
import com.yjt.fresh.api.ApiGetAddressList;
import com.yjt.fresh.base.BaseSimpleActivity;
import com.yjt.fresh.bean.BeanAddress;
import com.yjt.fresh.constant.MyConstants;
import com.yjt.fresh.utils.PreferenceManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 收货地址列表
 * Created by mcc on 2018-08-01.
 */

public class AtyAddresses extends BaseSimpleActivity {
    private static final int REQUEST_CODE = 1008;
    @BindView(R.id.rlBack)
    RelativeLayout mRlBack;
    @BindView(R.id.rvAppAtyAddresses)
    RecyclerView mRv;
    @BindView(R.id.tvAppAtyAddressesAppendNewAddress)
    TextView mTvAppendNewAddress;
    private AdpRvAtyAddresses mAdpAddress;

    @Override
    protected int getContentView() {
        return R.layout.app_aty_addresses;
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void initEvent() {
        initRV();
        getData();
        initClick();
    }

    private void initClick() {
        mRlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTvAppendNewAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext,AtyAppendAddress.class);
                intent.putExtra(MyConstants.ADD_OR_UPDATE_ADDRESS,MyConstants.ADD);
                startActivityForResult(intent,REQUEST_CODE);
            }
        });

        mAdpAddress.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.cbAppRecycleItemAtyAddress){//CheckBox
                    ((CheckBox)view).setChecked(true);
                    Intent intent = new Intent();
                    intent.putExtra(MyConstants.ADDRESS_OBJ,mBeanAddressList.get(position));
                    setResult(MyConstants.CHOOSE_ADDRESS,intent);
                    finish();
                }else if (view.getId() == R.id.tvAppRecycleItemAtyAddressAlter){//编辑
                    Intent intent = new Intent(mContext,AtyAppendAddress.class);
                    intent.putExtra(MyConstants.ADD_OR_UPDATE_ADDRESS,MyConstants.UPDATE);
                    intent.putExtra(MyConstants.ADDRESS_OBJ,mBeanAddressList.get(position));
                    startActivityForResult(intent,REQUEST_CODE);
                }
            }
        });
        mAdpAddress.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CheckBox cb = view.findViewById(R.id.cbAppRecycleItemAtyAddress);
                cb.setChecked(true);
                Intent intent = new Intent();
                intent.putExtra(MyConstants.ADDRESS_OBJ,mBeanAddressList.get(position));
                setResult(MyConstants.CHOOSE_ADDRESS,intent);
                finish();
            }
        });
    }

    private void initRV() {
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        mRv.setLayoutManager(manager);
        List<BeanAddress> list = new ArrayList<>();
        mAdpAddress = new AdpRvAtyAddresses(R.layout.app_recycle_item_address,list);
        mRv.setAdapter(mAdpAddress);
    }

    private List<BeanAddress> mBeanAddressList;
    HttpOnNextListener<List<BeanAddress>> mAddressListener = new HttpOnNextListener<List<BeanAddress>>() {
        @Override
        public void onNext(List<BeanAddress> beanAddresses) {
            mBeanAddressList = beanAddresses;
            mAdpAddress.setNewData(mBeanAddressList);
        }

        @Override
        public void onReLogin() {
            toLogin();
        }
    };
    private void getData() {
        ApiGetAddressList apiGetAddressList = new ApiGetAddressList(mAddressListener,mContext,true,false,false);
        Map<String,String> map = new HashMap<>();
        map.put("SESSION_ID", PreferenceManager.getInstance().getUserInfo().getSESSION_ID());
        apiGetAddressList.setMap(map);
        HttpManager.getInstance().doHttpDeal(apiGetAddressList);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getData();
    }
}
